package main

import (
	"fmt"
	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
	"github.com/sirupsen/logrus"
	format2 "go-logrus/format"
	"io"
	"os"
	"time"
)

func simple() {
	logrus.SetLevel(logrus.DebugLevel)
	fmt.Println("-------------level=", logrus.GetLevel())
	logrus.Error("出错误了")
	logrus.Warningln("警告")
	logrus.Infof("正常信息\n")
	logrus.Debugf("调试信息\n")

}

func format() {
	log := logrus.WithFields(logrus.Fields{
		"user_id": "21",
		"ip":      "127.0.0.1",
	})
	log.Errorf("-------------%s", "werw")
}
func color() {

	logrus.SetFormatter(
		&logrus.TextFormatter{
			FullTimestamp:   true,
			ForceColors:     true,
			TimestampFormat: "2000-01-01 15:23:23",
		})

	logrus.Error("出错误了")
	logrus.Warningln("警告")
	logrus.Infof("正常信息\n")
	logrus.Debugf("调试信息\n")

}

func print2file() {
	f, _ := os.OpenFile("D:/rrr.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	logrus.SetOutput(io.MultiWriter(f, os.Stdout))
	logrus.SetReportCaller(true)
	logrus.SetFormatter(&format2.MyFormatter{})

	logrus.Error("出错误了")
	logrus.Warningln("警告")
	logrus.Infof("正常信息\n")
	logrus.Debugf("调试信息\n")

}

func spliteLogFile() {

	logPath := "D:/logs/"
	fw, _ := rotatelogs.New(
		logPath+"logrus-%Y%m%d.log",
		rotatelogs.WithLinkName(logPath),
		rotatelogs.WithRotationCount(10),
		rotatelogs.WithRotationTime(time.Hour*24),
	)

	logrus.SetOutput(io.MultiWriter(fw, os.Stdout))
	logrus.SetReportCaller(true)
	logrus.SetFormatter(&format2.MyFormatter{})

	logrus.Error("出错误了")
	logrus.Warningln("警告")
	logrus.Infof("正常信息\n")
	logrus.Debugf("调试信息\n")
}

func main() {
	//simple()
	//format()
	//
	//color()

	//print2file()

	spliteLogFile()

}
